﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;
using System.Net;
using System.IO;

namespace RC1.teacher
{
    public partial class view_assignment : System.Web.UI.Page
    {
        int assignment_id;
        int class_id;

        protected void Page_Load(object sender, EventArgs e)
        {
            assignment_id = Convert.ToInt32(Session["assignment_id"]);
            class_id = Convert.ToInt32(Session["class_id"]);
            populatePage();
        }

        private void populatePage()
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();

            //Get info from DB (LINQ)
            var result = (from a in db.assignments
                          where a.assignment_id == this.assignment_id
                          select a).SingleOrDefault();

            //Write Lables
            Label1.Text = result.assignment_title;
            Label2.Text = result.assignment_description;
            Label3.Text = result.assignment_deadline.ToString();
            
            //Print remaining time
            if (result.assignment_deadline > DateTime.Now)
            {
                TimeSpan remaingTime = (DateTime)result.assignment_deadline - DateTime.Now;
                Label4.Text = remaingTime.Days.ToString() + " dagar, " + remaingTime.Hours.ToString() + " t, " + remaingTime.Minutes.ToString() + " m, " + remaingTime.Seconds.ToString() + " s";
                if (remaingTime.Days > 0)
                {
                    Label4.ForeColor = System.Drawing.Color.Green;
                }
                else
                {
                    Label4.ForeColor = System.Drawing.Color.Orange;
                }
            }
            else
            {
                Label4.Text = "Freistin er farin";
                Label4.ForeColor = System.Drawing.Color.Red;
            }

            //Print handed in submissions
            populateHandedInSubmission(db);
        }

        private void populateHandedInSubmission(tsu2011db4Entities1 db)
        {
            List<int> list = new List<int>();

            var handedIn = (from s in db.submissions
                            join ui in db.user_info on s.user_id equals ui.user_id
                            where s.assignment_id == assignment_id
                            select new {
                                id = s.submission_id,
                                user_id = s.user_id,
                                date = s.submission_date,
                                name = ui.user_info_first_name + " " + ui.user_info_last_name
                            });

            String table = "<table cellpadding='5' border='1' width='100%'>";
            table += "<tr><th>Navn</th><th>Dato</th><th>Støða</th><th>Rættað</th></tr>";
            foreach (var submission in handedIn)
            {
                //Finding variables
                String name = submission.name;
                DateTime date = submission.date;
                int id = submission.id;
                int user_id = submission.user_id;
                list.Add(user_id);
                String button = "<a href='view_submission.aspx?id="+id+"'>"+name +"</a>";
                String correctedString = "Ikki rættað";

                var corrected = (from cs in db.corrected_submission
                                 where cs.submission_id == id
                                 select cs).SingleOrDefault();
                if (corrected != null)
                {
                    correctedString = corrected.corrected_date.ToString();
                }


                table += "<tr><td>" + button + "</td><td>" + date.ToString() + "</td><td>Latið inn</td><td>" + correctedString + "</td></tr>";
            }

            var attending = (from a in db.attendings
                             join ui in db.user_info on a.user_id equals ui.user_id
                             where a.class_id == class_id
                             select new
                             {
                                 name = ui.user_info_first_name + " " + ui.user_info_last_name,
                                 user_id = a.user_id
                             });
            foreach (var student in attending)
            {
                String name = student.name;
                int user_id = student.user_id;
                if (!list.Contains(user_id))
                {
                    table += "<tr><td>" + name + "</td><td></td><td>Ikki latið inn</td><td></td></tr>";
                }
            }

            table += "</table>";
            Label5.Text = table;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Session["prePage"] = "view_assignment.aspx?id=" + assignment_id;
            Response.Redirect("edit_assignment.aspx?id=" + assignment_id);
        }
    }
}